#include "newtaskwidget.h"
#include "mainwindow.h"

NewTaskWidget::NewTaskWidget(MainWindow *parent) :
    QWidget(parent)
{
    parentWindow = parent;

    createLayout();
    createGroups();
    createWidgets();

    //connects
    connect(sourceButton, &QPushButton::clicked, this, &NewTaskWidget::sourceButtonClicked);
    connect(startBackup, &QPushButton::clicked, this,  &NewTaskWidget::startBackupClicked);
    connect(createArchive, &QCheckBox::toggled, compressionLevelValue,  &QComboBox::setEnabled);
    connect(encryptBackup, &QCheckBox::toggled, passwordValue,  &QComboBox::setEnabled);
    connect(periodic, &QCheckBox::toggled, periodDaysValue,  &QComboBox::setEnabled);
    connect(periodic, &QCheckBox::toggled, periodHoursValue,  &QComboBox::setEnabled);
    connect(periodic, &QCheckBox::toggled, firstRunValue,  &QComboBox::setEnabled);


    connect(backupTargetValue, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), backupTargets, &QStackedWidget::setCurrentIndex);
    this->setDefaultSettings();
}

void NewTaskWidget::createLayout()
{
    centralLayout = new QVBoxLayout();
    this->setLayout(centralLayout);

    sourceLayout = new QHBoxLayout();
    centralLayout->addLayout(sourceLayout);
}

void NewTaskWidget::createGroups()
{
    backupTypeGroup = new QGroupBox(tr("Backup type"));
    centralLayout->addWidget(backupTypeGroup);
    backupTypeLayout = new QGridLayout();
    backupTypeGroup->setLayout(backupTypeLayout);

    backupOptionsGroup = new QGroupBox(tr("Backup options"));
    centralLayout->addWidget(backupOptionsGroup);
    backupOptionsLayout = new QGridLayout();
    backupOptionsGroup->setLayout(backupOptionsLayout);

    backupTargetGroup = new QGroupBox(tr("Backup target"));
    centralLayout->addWidget(backupTargetGroup);
    backupTargetLayout = new QHBoxLayout();
    backupTargetGroup->setLayout(backupTargetLayout);
}

void NewTaskWidget::createWidgets()
{
    QLabel *sourceLabel = new QLabel(tr("Source: "));
    sourceLayout->addWidget(sourceLabel);

    sourceValue = new QLineEdit();
    sourceLayout->addWidget(sourceValue);

    sourceButton = new QPushButton(tr("..."));
    sourceLayout->addWidget(sourceButton);

    /* Backup type: Single or Cyclic */
    QLabel *backupTypeLabel = new QLabel(tr("Backup type: "));
    backupTypeLayout->addWidget(backupTypeLabel, 0, 0);

    backupTypeValue = new QComboBox();
    backupTypeLayout->addWidget(backupTypeValue, 0, 1);
    backupTypeValue->addItem(tr("Complete"));
    backupTypeValue->addItem(tr("Incremental"));
    backupTypeValue->addItem(tr("Differential"));

    /* Backup options */
    createArchive = new QCheckBox(tr("Create archive"));
    backupOptionsLayout->addWidget(createArchive, 0, 0, 1, 2);
    backupOptionsLayout->setColumnStretch(0, 1);
    backupOptionsLayout->setColumnStretch(1, 1);

    QLabel *compressionLevelLabel = new QLabel(tr("Compression level: "));
    backupOptionsLayout->addWidget(compressionLevelLabel, 0, 2);
    backupOptionsLayout->setColumnStretch(2, 1);
    compressionLevelValue = new QComboBox();
    backupOptionsLayout->addWidget(compressionLevelValue, 0, 3);
    backupOptionsLayout->setColumnStretch(3, 1);
    compressionLevelValue->addItem(tr("None"));
    compressionLevelValue->addItem(tr("Fastest"));
    compressionLevelValue->addItem(tr("Medium"));
    compressionLevelValue->addItem(tr("Maximum"));
    compressionLevelValue->setEnabled(false);

    encryptBackup = new QCheckBox(tr("Encrypt backup"));
    backupOptionsLayout->addWidget(encryptBackup, 1, 0, 1, 2);

    QLabel *passwordLabel = new QLabel(tr("Password:"));
    backupOptionsLayout->addWidget(passwordLabel, 1, 2);
    passwordValue = new QLineEdit();
    passwordValue->setEnabled(false);
    passwordValue->setEchoMode(QLineEdit::Password);
    backupOptionsLayout->addWidget(passwordValue, 1, 3);

    useHashes = new QCheckBox(tr("Calculate file hashes (slow)"));
    backupOptionsLayout->addWidget(useHashes, 2, 0);

    periodic = new QCheckBox(tr("Automatic scheduling"));
    backupOptionsLayout->addWidget(periodic, 3, 0);

    QLabel *periodLabel = new QLabel(tr("Period"));
    backupOptionsLayout->addWidget(periodLabel, 4, 0);

    QHBoxLayout *periodLayout = new QHBoxLayout();
    backupOptionsLayout->addLayout(periodLayout, 4, 2, 1, 2);
    periodDaysValue = new QSpinBox();
    periodDaysValue->setValue(1);
    periodDaysValue->setEnabled(false);
    periodLayout->addWidget(periodDaysValue);
    QLabel *periodDaysLabel = new QLabel(tr("Days"));
    periodLayout->addWidget(periodDaysLabel);
    periodHoursValue = new QSpinBox();
    periodHoursValue->setValue(0);
    periodHoursValue->setEnabled(false);
    periodLayout->addWidget(periodHoursValue);
    QLabel *periodHoursLabel = new QLabel(tr("Hours"));
    periodLayout->addWidget(periodHoursLabel);

    QLabel *firstRunLabel = new QLabel(tr("First run at:"));
    backupOptionsLayout->addWidget(firstRunLabel, 5, 0);

    firstRunValue = new QDateTimeEdit();
    firstRunValue->setCalendarPopup(true);
    firstRunValue->setDateTime(QDateTime::currentDateTime());
    firstRunValue->setEnabled(false);
    backupOptionsLayout->addWidget(firstRunValue, 5, 2);

    QLabel *backupTargetLabel = new QLabel(tr("Where to save?"));
    backupTargetLayout->addWidget(backupTargetLabel);

    backupTargetValue = new QComboBox();
    backupTargetLayout->addWidget(backupTargetValue);
    backupTargetValue->addItem(tr("Local disk"));
    //backupTargetValue->addItem(tr("SMB Remote disk"));
    backupTargetValue->addItem(tr("FTP"));
    backupTargetValue->addItem(tr("SSH"));

    backupTargets = new QStackedWidget();
    backupTargetLayout->addWidget(backupTargets);
    backupTargets->addWidget(localStorageWidget = new LocalStorageWidget);
    backupTargets->addWidget(ftpStorageWidget = new FTPStorageWidget);

    startBackup = new QPushButton(tr("Create task"));
    startBackup->setObjectName("genericButton");
    centralLayout->addWidget(startBackup, 0, Qt::AlignCenter);
}

void NewTaskWidget::setDefaultSettings()
{

}

void NewTaskWidget::sourceButtonClicked()
{
    QFileDialog dialog(this, tr("Select direcory you want to backup..."));
    dialog.setFileMode(QFileDialog::DirectoryOnly);
    if(dialog.exec() == QFileDialog::Accepted)
    {
        sourceValue->setText(dialog.selectedFiles().first());
    }
}

void NewTaskWidget::startBackupClicked()
{
    BackupTask task;

    if(sourceValue->text().at(sourceValue->text().length()-1) == '/') {
        sourceValue->setText(sourceValue->text().left(sourceValue->text().length()-1));
    }
    task.setSourcePath(sourceValue->text());

    if(backupTypeValue->currentIndex() == 0) {
        task.setBackupType(BackupTask::Complete);
    } else if (backupTypeValue->currentIndex() == 1) {
        task.setBackupType(BackupTask::Incremental);
    } else if (backupTypeValue->currentIndex() == 2) {
        task.setBackupType(BackupTask::Differential);
    } else {
        qWarning() << "Invalid selection";
    }

    task.setArchived(createArchive->isChecked());
    if(createArchive->isChecked()){
        if(compressionLevelValue->currentIndex() == 0){
            task.setCompressionType(BackupTask::NoCompression);
        }else if(compressionLevelValue->currentIndex() == 1){
            task.setCompressionType(BackupTask::Fastest);
        }else if(compressionLevelValue->currentIndex() == 2){
            task.setCompressionType(BackupTask::Medium);
        }else if(compressionLevelValue->currentIndex() == 3){
            task.setCompressionType(BackupTask::Maximum);
        }
    }

    task.setPeriodic(periodic->isChecked());
    if(periodic->isChecked()) {
        task.setInterval(24*periodDaysValue->value() + periodHoursValue->value());
        task.setNextRun(firstRunValue->dateTime());
    }

    if(backupTargetValue->currentIndex() == 0) {
        task.setBackupTargetType(BackupTask::Local);
        if(localStorageWidget->getTargetPath().at(localStorageWidget->getTargetPath().length()-1) == '/') {
            task.setTargetPath(localStorageWidget->getTargetPath().left(localStorageWidget->getTargetPath().length()-1));
        } else {
            task.setTargetPath(localStorageWidget->getTargetPath());
        }
    } else if(backupTargetValue->currentIndex() == 1) {
        task.setBackupTargetType(BackupTask::Ftp);
        task.setRemoteOptions(ftpStorageWidget->getHost(),
                               ftpStorageWidget->getLogin(),
                               ftpStorageWidget->getPass(),
                               ftpStorageWidget->getDir());
    } else if(backupTargetValue->currentIndex() == 2) {
        task.setBackupTargetType(BackupTask::Ssh);
    }

    task.createTask();
    emit newTaskCreated();
}
